import config from "@/focusSolid/path/config";
import { onMount, onCleanup } from "solid-js";
import { FocusContext, getFocusContext } from "@/focusSolid/store/focus";
import lodash from "lodash";
import { SolidFC } from "solid-shims";


const Index: SolidFC = function (props) {
  const [state, options] = getFocusContext()
  function onSwitchKeyDown(ev: KeyboardEvent) {
    options.triggerKeyDown(ev)
  }
  const throttleOnKeyDown = lodash.throttle(onSwitchKeyDown, config.throttleKeyDownTime)
  onMount(() => {
    window.addEventListener("keydown", throttleOnKeyDown)
  })
  onCleanup(() => {
    window.removeEventListener("keydown", throttleOnKeyDown)
  })
  return (
    <FocusContext.Provider value={[state, options]}>
      {props.children}
    </FocusContext.Provider>
  );
}
export default Index